/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.util.homogenizer;

import com.google.common.base.Charsets;
import langman.business.data.TranslationException;
import langman.util.languagefile.commons.TranslationMode;
import langman.util.languagefile.reader.LanguageReader;
import langman.util.languagefile.reader.LanguageReaderEventHandler;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;

public class ReferenceLoader implements LanguageReaderEventHandler, AutoCloseable {
    private final Collection<String> encounteredKeys = new ArrayList<>();

    private String currentCategory;

    private final BufferedReader fileReader;

    private final ReferenceDescription referenceDescription = new ReferenceDescription();

    public ReferenceLoader(File referenceFile) throws IOException {
        this(Files.newBufferedReader(referenceFile.toPath(), Charsets.UTF_8));
    }

    public ReferenceLoader(BufferedReader fileReader) {
        this.fileReader = fileReader;
    }


    public ReferenceDescription loadLanguage() throws TranslationException {
        try (LanguageReader reader = new LanguageReader(fileReader)) {
            reader.addListener(this);
            reader.readToEnd();
        } catch (IOException e) {
            throw new TranslationException(e);
        }
        return referenceDescription;
    }

    @Override
    public void categoryChanged(String newCategory) throws TranslationException {
        currentCategory = newCategory;
        if (referenceDescription.hasCategory(newCategory)) {
            throw new TranslationException("Category " + newCategory + " is defined twice in reference!");
        }
        referenceDescription.addCategory(newCategory);
    }

    @Override
    public void translationEncountered(String key, String value, TranslationMode translationMode) throws TranslationException {
        if (encounteredKeys.contains(key)) {
            throw new TranslationException("The key " + key + " is defined multiple times in reference language!");
        }
        encounteredKeys.add(key);

        referenceDescription.translate(currentCategory, key, value, translationMode);

    }

    @Override
    public void close() throws Exception {
        fileReader.close();
    }
}
